<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrator_label extends MY_Administrator {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();		
		//Load Model
		$this->load->model('administrator_label_model');
    }

	/*Content Management*/
	public function content_management($module=NULL,$arg=NULL)
	{
		switch($module):
			case 'add':		
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_label_model->content_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
				
				$this->data['content'] = "administrator/label/content_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);
				//Check Id exist
				$is_exist = $this->db->from('label')
				->where(array('label_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');	
								
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_label_model->content_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;							

				$this->data['dataResult'] = $this->db->get_where('label as t',array('t.label_id' => $id),1)->row();
	
				$this->data['content'] = "administrator/label/content_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;			
			case 'delete':
				$this->administrator_label_model->content_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;												
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "label_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case 'create_date':
						case 'update_date':
							$orderCondition = "t.".$orderby." ".$sort;
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
				
				
				/*Search Condition*/
				$searchCondition = array('t.label_id IS NOT NULL');
				$label_content_id = $this->input->get('label_id');
				$label_content_name = addslashes($this->input->get('label_name'));
				$update_by = addslashes($this->input->get('update_by'));
				if(!empty($label_content_id)) $searchCondition[] = "t.label_id LIKE '%".$label_content_id."%'";
				if(!empty($label_content_name)) $searchCondition[] = "t.label_name LIKE '%".$label_content_name."%'";
				if(!empty($update_by)) $searchCondition[] = "t.update_by LIKE '%".$update_by."%'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('label as t')->where(''.implode(' AND ',$searchCondition).'')->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->order_by($orderCondition)->get_where('label as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();
				
				$this->data['content'] = "administrator/label/content_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}	
}